<template>
  <div class="login-container">
    <el-form class="login-form" :rules="loginRules" ref="loginFormRef">
      <div class="admin-logo">
        <img class="logo" src="@/assets/vue.svg" alt="logo" />
        <h1 class="name">Vue3 Admin</h1>
      </div>
      <el-form-item prop="username">
        <el-input
          placeholder="请输入用户名"
          ref="username"
          v-model="loginForm.username"
        >
          <template #prepend>
            <span class="svg-container">
              <svg-icon icon-name="ant-design:user-outlined" />
            </span>
          </template>
        </el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          type="password"
          placeholder="请输入密码"
          autocomplete="on"
          show-password
          v-modle="loginForm.password"
        >
          <template #prepend>
            <span class="svg-container">
              <svg-icon icon-name="ant-design:lock-outlined" />
            </span>
          </template>
        </el-input>
      </el-form-item>
      <!-- 登录按钮 -->
      <el-button
        type="primary"
        w-full
        mb-30px
        :loading="loading"
        @click="handleLogin"
        >登录</el-button
      >
    </el-form>
  </div>
</template>

<script setup lang="ts">
import { FormInstance } from "element-plus"
import { useTemplateRef } from "vue"
import { useUserStore } from "@/stores/user"
import useRouteQuery from "@/hooks/useRouteQuery"

const loading = ref(false)
const loginFormRef = useTemplateRef<FormInstance>("Form")
const passworRef = useTemplateRef<HTMLInputElement>("password")
const usernameRef = useTemplateRef<HTMLInputElement>("username")
const userStore = useUserStore()
const router = useRouter()

const loginState = reactive({
  loginForm: {
    username: "",
    password: ""
  },
  loginRules: {
    username: [
      {
        required: true,
        tirgger: "blur",
        message: "请输入用户名！"
      }
    ],
    password: [
      {
        required: true,
        trigger: "blur",
        message: "请输入密码！"
      }
    ]
  }
})

const { proxy } = getCurrentInstance()!
const { redirect, otherQuery } = useRouteQuery()
const handleLogin = () => {
  loginFormRef.value?.validate(async (valid) => {
    if (valid) {
      loading.value = true
      try {
        await userStore.login(loginForm.value)
        proxy?.$message.success("登录成功！")
        router.push({
          path: redirect.value || "/",
          query: otherQuery.value
        })
      } finally {
        loading.value = false
      }
    } else {
      console.log("error submit")
    }
  })
}

onMounted(() => {
  if (loginState.loginForm.username === "") {
    usernameRef.value?.focus()
  } else if (loginState.loginForm.password === "") {
    passworRef.value?.focus()
  }
})
const { loginForm, loginRules } = toRefs(loginState)
</script>

<style lang="scss" scoped>
.login-container {
  @apply min-h-screen w-full overflow-hidden;
  .login-form {
    @apply relative w-[500px] max-w-full mx-auto pt-[140px] pb-[30px] overflow-hidden box-border;
    .admin-logo {
      @apply flex items-center justify-center mb-[20px];
      .logo {
        @apply w-[60px] h-[60px];
      }
      .name {
        @apply font-normal ml-[10px];
      }
    }
    .el-form-item {
      @apply mb-[20px];
      .el-input {
        @apply h-[40px];
      }
    }
  }
}
</style>
